package vacuum.lgadmin;

import java.util.HashMap;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;

import vacuum.lgadmin.jail.JailPlugin;
import vacuum.lgadmin.monsterspawn.MonsterSpawnPlugin;
import vacuum.lgadmin.performance.PerformancePlugin;
import vacuum.lgadmin.punishment.PunishmentPlugin;

public class LGAdmin extends JavaPlugin
{
	
	HashMap<CommandData, CommandExecutor> commands = new HashMap<CommandData, CommandExecutor>();
	
	String configURL = "http://vacuum-lgadministrate.googlecode.com/svn/trunk/resources/config.yml";
	
	LGPlugin[] plugins = {
			new MonsterSpawnPlugin(this),
			new PunishmentPlugin(this),
			new PerformancePlugin(this),
			new JailPlugin(this),
	};

	@Override
	public void onDisable()
	{
		
		log("Disabling LGAdmin plugin suite.");
		
		for (LGPlugin plugin : plugins)
		{
			log("Disabling " + plugin.getFullName() + "...");
			plugin.onDisable();
			log(plugin.getFullName() + " disabled!");
		}
		
		log("LGAdmin disabled!");
	}

	@Override
	public void onEnable()
	{
		log("Enabling LGAdmin " + this.getDescription().getVersion() + " plugin suite.");
		
		log("Verifying files.");
		FileManagement.verifyFiles(this, getFile());
		
		for (LGPlugin plugin : plugins)
		{

			log("Enabling " + plugin.getFullName() + "...");
			plugin.onEnable();
			log(plugin.getFullName() + " enabled!");
		}
		
		log("LGAdmin enabled!");
	}
	
	@Override
	public boolean onCommand(CommandSender sender, Command command,
			String label, String[] args)
	{
		for(CommandData key : commands.keySet()){
			if(label.equalsIgnoreCase(key.getCommand()) && sender.hasPermission(key.getPermission())){
				commands.get(key).onCommand(sender, command, label, args);
			}
		}
		return true;
	}

	void log(String s){
		System.out.println("[LGAdmin] " + s);
	}

	public void registerCommand(CommandData data, CommandExecutor exec)
	{
		commands.put(data, exec);
	}

	public void removeCommand(CommandData data)
	{
		commands.remove(data);
	}

}
